<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>开关灯</button>
    <script>
      var btn = document.querySelector("button");
      var body = document.body;
      var flag = 1; //设置一个变量表示关灯
      btn.onclick = function () {
        if (flag == 1) {
          body.style.backgroundColor = "black";
          flag = 0;
        } else {
          body.style.backgroundColor = "#fff";
          flag = 1;
        }
      };
    </script>
  </body>
</html>
